package com.zhu.lock;

import java.util.concurrent.locks.AbstractQueuedSynchronizer;

/**
 * @ClassName BinSync
 * @Description TODO
 * @Author zhuxiongbin
 * @Date 2021-10-28 15:46
 * @Version 1.0.0
 */
public class BinSync extends AbstractQueuedSynchronizer {


    @Override
    protected boolean tryAcquire(int arg) {
        if(getState() == 0 && compareAndSetState(0, arg)){
            setExclusiveOwnerThread(Thread.currentThread());
            return true;
        }

        return false;
    }


    @Override
    protected boolean tryRelease(int arg) {
        setExclusiveOwnerThread(null);
        setState(0);
        return true;
    }




}
